/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.gatech.gellinservlet.model;

import java.util.ArrayList;
import java.io.*;
import java.io.Serializable;
import java.util.logging.Level;

/**
 *
 * @author Tyler Powell
 */
public class LabManager extends DataManager<Lab> implements Serializable{
    
    public LabManager()
    {
        super();
    }
    
    public Lab createLab(String name, int teamMaxSize, int teamDesignation, 
            int courseID, ArrayList<Student> teamlessStudents)
    {
        Lab lab = new Lab(name, teamMaxSize, teamDesignation, courseID, teamlessStudents);
        add(lab);
        return lab;
    }
    
    //TODO Change below method to return team, add team to teamManagers in outside of this class
    //such as makeLab.jsp
    
    public Team addTeamToLab(String name, int labID, TeamManager teamData)
    {
        Lab aLab = search(labID);
        Team aTeam = null;
        if(aLab.getTeamDesignation() != Lab.NOTEAM)
        {
            aTeam = new Team(name, aLab.getTeamMaxSize(), aLab.getTeamDesignation());

            if(aLab.getTeamDesignation() == Lab.RANDOMTEAM )
            {
                aTeam.populate(aLab.getTeamlessStudents());
            }
            aLab.addTeam(aTeam);
        }
        return aTeam;
    }
    
    public void removeTeamFromLab(Team aTeam, int labID)
    {
        Lab aLab = search(labID);
        aLab.removeTeam(aTeam);
    }
    
/**
      * Save this object via serialization
      * 
      * @param filename the name of the file to save to
      */
     public void save(String filename) {
         try {
             /*
              * Create the object output stream for serialization.
              * We are wrapping a FileOutputStream since we
              * want to save to disk.  You can also save to socket
              * streams or any other kind of stream.
              */
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
            
            /*
             * The only real call we need.  The stream buffers the output and reuses
             * data, so if you are serializing very frequently, then the object values might
             * not actually change because the old serialized object is being reused.
             * 
             * To fix this you can try writeUnshared() or you can reset the stream.
             * out.reset();
             */
            out.writeObject(this.getDataList());
            //out.writeObject(this.getDataList()); //this writes out the members, so we know that works
        } catch (FileNotFoundException e) {
            Log.getInstance().log(Level.SEVERE, "Save file not found: " + filename, e);
        } catch (IOException e) {
            Log.getInstance().log(Level.SEVERE, "General IO Error on saving: " + filename, e);
        }
         
     }
     
     /**
     * This is an example of a factory method
     * We call this static method to create an
     * instance of UserManager from a serialized file.
     *
     * @param filename the name of the file to use
     * @return an instance of UserManager initialized from a file.
     */
     public void getFromFile(String filename) {
         ArrayList c = null;
         try {
             /*
              * Create the input stream.  Since we want to read from the disk,
              * we wrap a file stream.
              */
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
            /*
             * Now we can read the entire company in with only one call
             */
            c = (ArrayList) in.readObject();
            this.setDataList(c);

        } catch (FileNotFoundException e) {
            Log.getInstance().log(Level.SEVERE, "Load file not found: " + filename, e);
        } catch (IOException e) {
            Log.getInstance().log(Level.SEVERE, "General IO Error on loading: " + filename, e);
        } catch (ClassNotFoundException e) {
            Log.getInstance().log(Level.SEVERE, "Company class not found on loading: " + filename, e);
        }
        
     }
}
